Magnetic resonance imaging (MRI) employs three types of magnetic fields, a strong static magnetic field B.sub.0 to polarize the sample, a radiofrequency (RF) field B.sub.1 for signal excitation, and a vector gradient field G=(G.sub.x, G.sub.y, G.sub.z) for spatial encoding. Medical MRI depends on homogeneous magnetic fields in order to produce anatomically correct and medically useful images. In the presence of implanted metal devices, however, all three magnetic fields are distorted, resulting in images with serious artifacts. The metal devices causing image distortions include orthopedic hardware, joint replacements, and surgical staples. Other imaging modalities, such as CT, ultrasound, and radiography, are also impaired by artifact from the metal and do not allow adequate visualization of soft tissue structures. The magnetic fields may also be distorted by nonmetallic frames used in surgical planning. The frames are needed for accurate coordinate determination for a surgery, and any geometrical distortion, or "warping", of the magnetic resonance (MR) images due to intrinsic sensitivity of MRI to magnetic field inhomogeneity must be corrected. Furthermore, interfaces between dissimilar substances may also cause magnetic field distortions due to discontinuity in magnetic susceptibility. For example, air/tissue interfaces in the lungs often cause distortion of MR images. Thus, at present, it is often impossible to obtain clinically useful MR images from anatomic areas near implanted metal and nonmetallic devices or air/tissue interfaces. There are many clinical circumstances, however, in which the structures adjacent a distortion-causing factor, such as metal, need to be evaluated. These circumstances include monitoring joint replacement complications, evaluating the spine/nerve roots after spinal instrumentation, and monitoring possible tumor recurrence after bone resection and reconstruction. Thus, a need exists for a method of correcting for magnetic field inhomogeneity in MRI that produces MR images of a medically reliable quality.